package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.entity.BpProductionFacilityVideo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.List;

/**
 * @author xsx
 * @version 1.0
 * @date 2022/10/12 11:38
 */
public interface BpProductionFacilityVideoDao extends JpaRepository<BpProductionFacilityVideo, Long>, QuerydslPredicateExecutor<BpProductionFacilityVideo> {


    @Query("select v from BpProductionFacilityVideo v where v.deleteFlag='0' and v.deviceFacilityId=?1")
    List<BpProductionFacilityVideo> findListByDeviceFacilityId(Long deviceFacilityId);

    BpProductionFacilityVideo findByDeviceFacilityIdAndSystemVideoId(Long deviceFacilityId,Long systemVideoId);

    @Query("select v.systemVideoId from BpProductionFacilityVideo v where v.deleteFlag='0' and v.deviceFacilityId in (?1)")
    List<Long> findByFacilityIds(List<Long> facilityIds);

    @Query("select v.systemVideoId from BpProductionFacilityVideo v where v.deleteFlag='0' and v.deviceFacilityId =?1")
    List<Long> findByFacilityId(Long facilityId);

    @Query("select v.systemVideoId from BpProductionFacilityVideo v where v.deleteFlag='0' and v.deviceFacilityId=?1")
    List<Long> findIdsByDeviceFacilityId(Long deviceFacilityId);

    @Query(value = "SELECT fv.* FROM\twwyt_bp_production_facility_video fv LEFT JOIN wwyt_bp_system_video v ON fv.system_video_id = v.id " +
            "WHERE fv.DELETE_FLAG = '0'  AND v.DELETE_FLAG = '0' and fv.device_facility_id=?1",nativeQuery = true)
    List<Long> findByDeviceFacilityId(Long deviceFacilityId);

    @Modifying
    @Query(value = "update wwyt_bp_production_facility_video set DELETE_FLAG='1' where device_facility_id=?1",nativeQuery = true)
    void deleteByDeviceFacilityId(Long deviceFacilityId);
}
